import type { GetOrgTreeParams, GetUserListParams, GetUserParams, OrgBo, ResponseBo, TransferUsers, UserInfoBO } from '@/model/UserBo';
import * as UserApi from '@/api/UserApi';

export class UserService {
  /**
   * 获取用户列表
   * @param data
   * @returns 用户列表和页码数据
   */
  public static async getUserList(data: GetUserListParams): Promise<UserInfoBO> {
    try {
      const response = await UserApi.getUserList(data);
      const finalUserData = response.data?.records?.map((item: any, index: number) => ({
        ...item,
        index: index + 1,
      }));
      const finalData = {
        ...response.data,
        records: finalUserData,
      };
      return finalData;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      current: 1,
      pages: 1,
      records: [],
      size: 10,
      total: 0,
    };
  }

  /**
   * 变更用户全部组织下的状态
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 变更用户全部组织下的状态
   */
  public static async changAllUserStatus(data: GetUserParams): Promise<ResponseBo> {
    try {
      const response = await UserApi.changAllUserStatus(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }

  /**
   * 变更用户状态
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 变更用户状态
   */
  public static async changUserStatus(data: TransferUsers): Promise<ResponseBo> {
    try {
      const response = await UserApi.changUserStatus(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }

  /**
   * 获取组织机构树
   * @param data
   * @returns 组织机构树
   */
  public static async getOrgTreeOriginalData(data: GetOrgTreeParams): Promise<any> {
    try {
      const response = await UserApi.getOrgTreeOriginalData(data);
      return response.data;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {};
  }

  /**
   * 加载补充信息接口
   * @param data
   * @returns string, 补充信息
   */
  public static async getUserExtraInfo(data: GetUserParams): Promise<string> {
    try {
      const response = await UserApi.getUserExtraInfo(data);
      return response.data?.extraInfo;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return '';
  }

  /**
   * 保存补充信息
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 保存补充信息
   */
  public static async updateUserExtraInfo(data: GetUserParams): Promise<ResponseBo> {
    try {
      const response = await UserApi.updateUserExtraInfo(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }

  /**
   * 获取用户组织列表
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 用户组织列表数据
   */
  public static async getUserOrgList(data: GetUserParams): Promise<OrgBo[]> {
    try {
      const response = await UserApi.getUserOrgList(data);
      const orgList = response.data ?? [];
      return orgList.map((item: any, index: number) => ({
        ...item,
        index: index + 1,
      }));
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return [];
  }

  /**
   * 用户下线
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 用户下线
   */
  public static async offlineUser(data: TransferUsers): Promise<ResponseBo> {
    try {
      const response = await UserApi.offlineUser(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }

  /**
   * 从本组织中移除用户
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 从本组织中移除用户
   */
  public static async removeUser(data: GetUserParams[]): Promise<ResponseBo> {
    try {
      const response = await UserApi.removeUser(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }

  /**
   * 保存组织用户移入
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 保存组织用户移入
   */
  public static async transferUsersIn(data: TransferUsers): Promise<ResponseBo> {
    try {
      const response = await UserApi.transferUsersIn(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }

  /**
   * 保存组织用户转出至其他组织
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 保存组织用户转出至其他组织
   */
  public static async transferUsersOut(data: TransferUsers): Promise<ResponseBo> {
    try {
      const response = await UserApi.transferUsersOut(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }

  /**
   * 重置密码
   * @param data
   * @returns ResponseBo<{ code: string; msg: string; data: T }> 重置密码
   */
  public static async resetPassword(data: GetUserParams): Promise<ResponseBo> {
    try {
      const response = await UserApi.resetPassword(data);
      return response;
    } catch (err) {
      console.error('接口调用失败:', err);
    }
    return {
      code: '1',
      message: '接口调用失败',
      data: '',
    };
  }
}
